#!/bin/bash

# Abort script at first error, so that the script doesn't exit with
# status 0 for missing files 
set -o errexit

if [ $# -ne 1 ] && [ $# -ne 2 ]; then
	echo "Simple packaging file for the Windows port.  Only really useful when"
	echo "cross-compiling, or using Cygwin.  Originally by Robert Ruehlmann."
	echo ""
	echo "Use in the root directory of the game, like so:"
	echo "   scripts/pkg_win <name of of release, e.g. angband-4.0.1> [<output filename>]"
	echo ""

	exit 1
fi

DIR=$1
if [ $# -eq 1 ]; then
	OUT=$1.zip
else
	OUT=$2
fi


function cp_unix2dos {
  # Just in case they're already DOS_format, we strip them to UNIX first. :)
  awk '{ sub("\r$", ""); sub("$", "\r"); print }' $1 > $2
}


mkdir -p $DIR
cd $DIR

mkdir lib
mkdir lib/gamedata
mkdir lib/customize
mkdir lib/help
mkdir lib/screens
mkdir lib/fonts
mkdir lib/tiles
mkdir lib/tiles/adam-bolt
mkdir lib/tiles/gervais
mkdir lib/tiles/nomad
mkdir lib/tiles/old
mkdir lib/tiles/shockbolt
mkdir lib/sounds
mkdir lib/icons
mkdir lib/user
mkdir lib/user/save
mkdir lib/user/scores
mkdir lib/user/info
mkdir lib/user/archive

touch lib/user/delete.me lib/user/save/delete.me lib/user/scores/delete.me \
      lib/user/info/delete.me lib/user/archive/delete.me

cp ../*.exe .
cp ../*.dll .

# Copy the readmes and suchlike, converting to DOS line endings on the way
cp_unix2dos ../changes.txt ./Changes.txt
cp_unix2dos ../thanks.txt ./Thanks.txt
cp_unix2dos ../copying.txt ./Copying.txt
cp_unix2dos ../faq.txt ./FAQ.txt
cp ../doc/manual.pdf ./Manual.pdf
cp ../doc/manual.html ./Manual.html

cp ../lib/readme.txt lib/

cp ../lib/gamedata/*.txt lib/gamedata
cp ../lib/screens/*.txt lib/screens

cp ../lib/help/*.txt lib/help
cp ../lib/help/*.hlp lib/help

cp ../lib/customize/*.prf lib/customize

cp ../lib/fonts/*.fon lib/fonts

cp ../lib/tiles/*.txt lib/tiles
cp ../lib/tiles/adam-bolt/* lib/tiles/adam-bolt
cp ../lib/tiles/gervais/* lib/tiles/gervais
cp ../lib/tiles/nomad/* lib/tiles/nomad
cp ../lib/tiles/old/* lib/tiles/old
cp ../lib/tiles/shockbolt/* lib/tiles/shockbolt
rm lib/tiles/*/Makefile

cp ../lib/sounds/sound.cfg lib/sounds
cp ../lib/sounds/*.mp3 lib/sounds

cp ../lib/user/info/*.txt lib/user/info
cp ../lib/user/info/*.hlp lib/user/info

cd ..
zip -9 -r $OUT $DIR
rm -rf $DIR
